package com.ruoyi.projects.apis;

import com.ruoyi.common.annotation.IgnoreAuth;
import com.ruoyi.common.annotation.LoginUserInfo;
import com.ruoyi.common.core.controller.ApiBaseController;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.projects.Vos.SlSchedulesRecord;
import com.ruoyi.projects.Vos.WxUserInfoDTO;
import com.ruoyi.projects.domain.SlMemberCard;
import com.ruoyi.projects.domain.SlMemberCardRecord;
import com.ruoyi.projects.domain.SlSchedules;
import com.ruoyi.projects.dto.ProductTypeDict;
import com.ruoyi.projects.dto.SlMemberCardVo;
import com.ruoyi.projects.service.ISlMemberCardRecordService;
import com.ruoyi.projects.service.ISlMemberCardService;
import com.ruoyi.projects.service.ISlProductService;
import com.ruoyi.projects.service.ISlSchedulesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers.data;

/**
 * 会员卡相关接口
 */
@RestController
@RequestMapping("/api/cards")
public class UserCardApisApis extends ApiBaseController {

    private static final Logger log = LoggerFactory.getLogger(UserCardApisApis.class);

    @Autowired
    private ISlMemberCardService slMemberCardService;

    @Autowired
    ISlSchedulesService slSchedulesService;

    /**
     * 查询我的会员卡列表
     * @return
     */
    @PostMapping("/listMyCards")
    public ApiResult listMyCards(@LoginUserInfo WxUserInfoDTO dto)
    {
        List<SlMemberCard> data=slMemberCardService.listMyCards(dto.getId());
        if (StringUtils.isNull(data))
        {
            return ApiResult.success(new ArrayList<SlMemberCardVo>());
        }else {
            List<SlMemberCardVo> vos=new ArrayList<>();
            data.forEach(item ->{
                SlMemberCardVo slMemberCardVo = new SlMemberCardVo();
                BeanUtils.copyProperties(item,slMemberCardVo);
                slMemberCardVo.setId(item.getCardId());
                vos.add(slMemberCardVo);
            });
            return ApiResult.success(vos);
        }
    }

    /**
     * 查询我的会员卡列表
     * @return
     */
    @PostMapping("/cardById")
    public ApiResult cardById(@RequestBody SlMemberCardVo slMemberCardVo)
    {
        SlMemberCard slMemberCard = slMemberCardService.selectSlMemberCardByCardId(slMemberCardVo.getId());
        if (StringUtils.isNull(slMemberCard))
        {
            return ApiResult.success(null);
        }else {
            SlMemberCardVo cardVo = new SlMemberCardVo();
            BeanUtils.copyProperties(slMemberCard,cardVo);
            return ApiResult.success(cardVo);
        }
    }

    /**
     * 卡排期记录查询
     * @param dto
     * @return
     */
    @PostMapping("/schedules")
    public ApiResult schedules(@LoginUserInfo WxUserInfoDTO dto, @RequestBody SlMemberCardVo slMemberCardVo)
    {
        List<SlSchedulesRecord> data=slSchedulesService.schedules(slMemberCardVo.getId());
        if (StringUtils.isNull(data))
        {
            return ApiResult.success(new ArrayList<SlSchedulesRecord>());
        }else {
            List<SlSchedulesRecord> collect = data.stream()
                    // 自定义排序规则：先按pushStatus升序，再按schedulesTime升序
                    .sorted(
                            // 1. 第一步：按pushStatus排序（"0"在前，"1"在后）
                            Comparator.comparing(SlSchedulesRecord::getPushState)
                                    // 2. 第二步：同pushStatus下，按schedulesTime升序（时间早的在前）
                                    .thenComparing(SlSchedulesRecord::getSchedulesTime)
                    )
                    // 收集排序后的结果为List
                    .collect(Collectors.toList());
            return ApiResult.success(collect);
        }
    }

}
